package com.tanhua.app.controller;

import com.tanhua.UserHolder;
import com.tanhua.app.service.UserService;
import com.tanhua.db.User;
import com.tanhua.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;


    @GetMapping(value="/users",name="登录用户的数据回显")
    public ResponseEntity saveUser(Long userID,Long huanxinID){
        // 如果有：先根据userID和huanxinID做查询（后面要用）
        // 没有：根据id做查询
        if (userID != null) {
            // 1、根据userID查询
            return userService.findUserInfoVo(userID);
        } else if (huanxinID != null) {
            // 2、根据huanxinID查询
            return userService.findUserInfoVo(huanxinID);
        }else{
            //3 、根据登录用户的id查询
            User user = UserHolder.getUser();
            System.out.println(user);
            return userService.findUserInfoVo(user.getId());
        }
    }


    @PostMapping(value = "/users/header",name="修改用户的信息--头像")
    public ResponseEntity updateHeader(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        return userService.head(headPhoto,token);
    }

    @PutMapping(value = "/users",name="修改用户的信息--数据")
    public ResponseEntity updateData(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo) throws IOException {
        return userService.updateData(userInfo,token);
    }


    @GetMapping(value="/users/counts",name="互相喜欢，喜欢，粉丝 - 统计")
    public ResponseEntity counts(){
        return userService.counts();
    }


    @GetMapping(value="/users/friends/{type}",name="互相喜欢，喜欢，粉丝列表查询")
    public ResponseEntity findMyCountList(@PathVariable("type") Integer type,Integer page,Integer pagesize){
        return userService.findMyCountList(type,page,pagesize);
    }

    @PostMapping(value = "/users/fans/{uid}",name="粉丝变好友")
    public ResponseEntity fanstoFriend(@PathVariable("uid") Integer fanId) throws IOException {
        return userService.fanstoFriend(fanId);
    }

    @DeleteMapping(value = "/users/like/{uid}",name="取消喜欢(互相喜欢的取消或者拒接当舔狗)")
    public ResponseEntity removeLove(@PathVariable("uid") Integer loveId) throws IOException {
        return userService.removeLove(loveId);
    }

}
